package com.yuntongxun.ecdemo.voicechat.ui.services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.loopj.android.http.RequestParams;
import com.yuntongxun.ecdemo.common.CCPAppManager;
import com.yuntongxun.ecdemo.core.ClientUser;
import com.yuntongxun.ecdemo.storage.ContactSqlManager;
import com.yuntongxun.ecdemo.ui.contact.ECContacts;
import com.yuntongxun.ecdemo.voicechat.api.ApiConstants;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.presenter.ICommonRequestPresenter;
import com.yuntongxun.ecdemo.voicechat.presenter.impl.CommonRequestPresenterImpl;
import com.yuntongxun.ecdemo.voicechat.util.JsonHelper;
import com.yuntongxun.ecdemo.voicechat.util.iview.ICommonViewUi;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/16.
 */

public class MembersService extends Service implements ICommonViewUi {

    private ICommonRequestPresenter iCommonRequestPresenter = null;
    private List<UserInfo> mResultList = new ArrayList<UserInfo>();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        iCommonRequestPresenter = new CommonRequestPresenterImpl(this, this);
        toRequest(ApiConstants.EventTags.BEGIN_EVENT);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void toRequest(int eventTag) {
        RequestParams map = new RequestParams();
        map.put("page", "" + 1);
        map.put("pageSize", "99999");
        map.put("listAll", "0");
        iCommonRequestPresenter.request(ApiConstants.EventTags.BEGIN_EVENT, this, ApiConstants.Urls.ACCOUNT_LIST, map);
    }

    @Override
    public void getRequestData(int eventTag, String result) {
//        Log.e("getRequestData---",result);
        ClientUser user = CCPAppManager.getClientUser();
        if (user==null||user.getUserId()==null){
            return;
        }
        mResultList.clear();
        List<UserInfo> resultList = parseResult(result);
        mResultList.addAll(resultList);
        Log.e("size",mResultList.size()+"");
        List<ECContacts> contacts = new ArrayList<>();
        for (int i = 0; i < resultList.size(); i++) {
            ECContacts contact = new ECContacts();
            UserInfo info = mResultList.get(i);
//            String message = info.getNickName() + "-" + info.getGender() + "-" + info.getAge() + "-" + info.getCountry() + "-" + info.getNativeLanguage() + "-" + info.getLearningLanguage();
            StringBuffer message= new StringBuffer();
            message.append("{\"result\":" );
            message.append("{\"accountName\":\"" + info.getAccountName() + "\",");
            message.append("\"nickName\":\"" + info.getNickName() + "\",");
            message.append("\"id\":\"" + info.getId() + "\",");
            message.append("\"gender\":\"" + info.getGender() + "\",");
            message.append("\"age\":\"" + info.getAge() + "\",");
            message.append("\"country\":\"" + info.getCountry() + "\",");
            message.append("\"nativeLanguage\":\"" + info.getNativeLanguage() + "\",");
            message.append("\"learningLanguage\":\"" + info.getLearningLanguage() + "\"}}");
//            LogUtil.e("message",message.toString());
            contact.setNickname(mResultList.get(i).getNickName() + "(" + message.toString()+ ")");
            contact.setContactid(mResultList.get(i).getId());
            contacts.add(contact);
        }
        ContactSqlManager.insertContacts(contacts);
    }

    @Override
    public void onRequestSuccessException(int eventTag, String msg) {

    }

    @Override
    public void onRequestFailureException(int eventTag, String msg) {

    }

    @Override
    public void isRequesting(int eventTag, boolean status) {

    }

    public List<UserInfo> parseResult(String result) {
        JsonHelper<UserInfo> dataParser = new JsonHelper<UserInfo>(
                UserInfo.class);
        return dataParser.getDatas(
                result, "list");
    }
}
